// Copyright Notice:
//				number_ctrl.js v1.0.2
//			Copyright@2008 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
	OpenSiteMobile numeric input control w/ dojo integration
*/

dojo.provide("msos.number_ctrl");
dojo.require("msos.common");


msos.number_ctrl.tool = function(input_el) {
 this.num_max_val = 999;
 this.num_min_val = 0;
 this.num_start_val = input_el.value || 0;
 this.num_err_msg = '???';
 this.num_ctrl_left = 0;
 this.num_ctrl_top = 0;
 this.num_ctrl_id = 'number ctrl';
 this.num_allow_blank = true;
 this.num_container = null;
 this.after_change_function = function(nc_obj) { return false; }
 var num_obj = this;

 this.generate_container_div = function() {
       var container_div = document.createElement("div");
       var z_index	= input_el.style.zIndex || input_el.parentNode.style.zIndex || 100;
       container_div.style.zIndex = ++z_index;
       container_div.style.position = 'absolute';
       container_div.style.display = 'none';

       var set_container_position = function() {
	      var num_positn	= dojo._abs(input_el, true);
	      var num_width	= input_el.offsetWidth;
	      var n_left	= num_positn.x + num_width;
	      var n_top         = num_positn.y;
	      if (num_obj.num_ctrl_left) { n_left += num_obj.num_ctrl_left; }
	      if (num_obj.num_ctrl_top)  {  n_top += num_obj.num_ctrl_top;  }
              container_div.style.top =  n_top  + 'px';
              container_div.style.left = n_left + 'px';
              container_div.style.display = 'block';
       }

       container_div.container_position = set_container_position;

       input_el.parentNode.appendChild(container_div);
       dojo.connect(window, "onresize", null, set_container_position);
       return container_div;
 }

 this.generate_num_ctrl = function() {
       var num_but_up, num_but_dn;
       num_obj.num_container = num_obj.generate_container_div();
       // Increase input field
       num_but_up = new msos.common.generate_img_button(num_obj.num_container);
       num_but_up.img_url	= djConfig.msos_folder + '/images/';
       num_but_up.img_name	= 'up_arrow.gif';
       num_but_up.img_title	= '+1';
       num_but_up.img_alt	= '+1';
       num_but_up.img_size	= 'img_num';
       num_but_up.img_but_id	= num_obj.num_ctrl_id + ' up';
       num_but_up.button_mouseup = num_obj.increase;
       num_but_up.img_button_append();
       num_but_up.img_elm.style.display = 'block';
       // Decrease input field
       num_but_dn = new msos.common.generate_img_button(num_obj.num_container);
       num_but_dn.img_url	= djConfig.msos_folder + '/images/';
       num_but_dn.img_name	= 'dn_arrow.gif';
       num_but_dn.img_title	= '-1';
       num_but_dn.img_alt	= '-1';
       num_but_dn.img_size	= 'img_num';
       num_but_dn.img_but_id	= num_obj.num_ctrl_id + ' down';
       num_but_dn.button_mouseup = num_obj.decrease;
       num_but_dn.img_button_append();
       num_but_dn.img_elm.style.display = 'block';
       input_el.num_input_obj = num_obj;

       dojo.connect(input_el, "onkeypress",      null,	num_obj.num_onkeypress	);
       dojo.connect(input_el, "onmousewheel",    null,	num_obj.num_onwheel	);
       dojo.connect(input_el, "onblur",          null,	num_obj.num_onblur	);

       console.debug("num. ctrl: " + num_obj.num_ctrl_id + ", z-index -> " + num_obj.num_container.style.zIndex);
       setTimeout(num_obj.num_container.container_position, 100);
 }

 this.increase =	function() { num_obj.num_set_value(1);  }
 this.decrease =	function() { num_obj.num_set_value(-1); }
 this.num_onblur =	function() { num_obj.num_set_value(); }

 this.num_set_value = function(delta) {
       var flag_select = false;
       var flag_focus = delta ? true : false;    // no delta for on_blur so no focus at end

       console.debug('num_set_value: input value -> ' + input_el.value + ', delta -> ' + delta);

	// Several special cases
       switch (input_el.value) {
	      case 0:
	      case '+':
	      case '-':
		     if	(delta)				input_el.value = num_obj.compare_high_low(0 + delta);
		     break;
	      case '':
		     if	(delta)				input_el.value = num_obj.compare_high_low(0 + delta);
		     else if (!num_obj.num_allow_blank)	input_el.value = num_obj.num_start_val;
		     break;
	      case '0':
	      case '-0':
	      case '+0':
		     if	(delta)				input_el.value = num_obj.compare_high_low(0 + delta);
		     else					input_el.value = num_obj.compare_high_low(0);
		     break;
	      case num_obj.num_err_msg:
		     if	(delta)				input_el.value = num_obj.compare_high_low(0 + delta);
		     else					flag_select = true;
		     break;
	      default:
		     var in_value = parseInt(input_el.value) || null;
		     if (!in_value) {
			    input_el.value = num_obj.num_err_msg;
			    flag_select = true;
		     } else {
			    if (delta) in_value = in_value + delta;
			    in_value = num_obj.compare_high_low(in_value);
			    if (input_el.value != in_value)	{
				   input_el.value = in_value;
				   setTimeout(num_obj.after_change_function, 250);
			    }
		       }
       }
       if      (flag_select) { input_el.select(); }
       else if (flag_focus)  { input_el.focus();  }
 }

 this.num_onkeypress = function(evt) {
	var key = '';
	if	(evt.keyCode)	key = evt.keyCode;
	else if	(evt.which)	key = evt.which;
	var key_char = String.fromCharCode(key) || '';

	var debug_txt = 'num_onkeypress: ' + key + ' (' + key_char + ')';

 	// Don't allow these
	if (key >= 65) {
		input_el.value = num_obj.num_err_msg;
		setTimeout(num_obj.num_onblur, 250);
		console.debug(debug_txt + ' not allowed!');
		return false;
	}
	switch (key) {
		case null:
		case 0:
		case 8:  // backspace
		case 46: // delete
			setTimeout(num_obj.num_onblur, 250);
			return true;
		case 43: // plus
		case 45: // minus
			setTimeout(num_obj.num_onblur, 250);
			return true;
		case 40: //down arrow
			num_obj.num_set_value(-1);	// Don't need timeout since value is set in 'num_set_value'
			return true;
		case 38: //up arrow
			num_obj.num_set_value(1);
			return true;
	}
       if (/^\d$/.test(key_char)) {
	      setTimeout(num_obj.num_onblur, 250);	// Use 'num_onblur' so no delta is passed in to 'num_set_value'
	      return true;
       } else {
	      input_el.value = num_obj.num_err_msg;
	      setTimeout(num_obj.num_onblur, 250);
	      console.debug(debug_txt + ' not an integer!');
	      return false;
         }
 }

 this.num_onwheel = function(evt) {
       if	(evt.wheelDelta > 0) { num_obj.increase(); }
       else if (evt.wheelDelta < 0)  { num_obj.decrease(); }
 }

 this.compare_high_low = function(check_val) {
       if (check_val < num_obj.num_min_val) check_val = num_obj.num_min_val;
       if (check_val > num_obj.num_max_val) check_val = num_obj.num_max_val;
       return check_val;
 }

}
